local function BuildAttr(attr, curAttr)
    local desc = "";
    
    for k,v in ipairs(attr) do
        if v[2] > 0 then
            desc = desc .. GetUtf8(EXCEL.attr[v[1]].name) ..":"..v[2];
            if curAttr then
                local curV = 0;
                for k1,v1 in pairs(curAttr) do
                    if v1[1] == v[1] then
                        curV = v1[2];
                        break;
                    end
                end
                if curV < v[2] then
                    desc = desc .. " +" ..(v[2]-curV);
                end
            end
            desc = desc .. "\n";
        end
    end
   
    return desc; 
end

function EnterEquip(parent)
    local scene = LoadJson("equip.json");
    scene:setPosition(cc.p(0,0))
    parent:addChild(scene);
    
    scene.ui = scene.layer:getChildByName("Panel_1");
    LoadAllNode(scene, scene.ui);
    
    local id = nil;
    local curLv = nil;
    local maxLv = nil;
    local nextLv = nil;
    local curIntensify = nil;
    local nextIntensify = nil;
    
    local oldColor = scene.nextAttr:getColor();
    
    local function updateStatus()
        if not id then
            local isMax = false;
            for k,v in ipairs(EXCEL.equip) do
                local curLv = g_Player.equips[k];
                local maxLv = v.intensify[2]-v.intensify[1]+1;
                if not id then
                    id = k;
                    isMax = curLv >= maxLv;
                end
                if curLv < maxLv then
                    if isMax then
                        id = k;
                        isMax = false;
                    end
                    
                    local intensify = EXCEL.intensify[v.intensify[1]+curLv-1+1];
                    if g_Player.items[CONST.items.gold] >= intensify.gold then
                        id = k;
                        break;
                    end
                end
            end
        end
        
        for k,v in pairs(EXCEL.equip) do
            local name = "equip"..k;
            
            SetNodeEnable(scene[name], id~=k)
        end
        
        local equip = EXCEL.equip[id];
        curLv = g_Player.equips[id];
        maxLv = equip.intensify[2]-equip.intensify[1]+1;
        nextLv = curLv < maxLv and (curLv+1) or maxLv;
        curIntensify = EXCEL.intensify[equip.intensify[1] + curLv-1];
        nextIntensify = EXCEL.intensify[equip.intensify[1] + nextLv-1];
        local item = EXCEL.item[equip.item];
        scene.currentEquip:loadTexture(item.pathUI);

        scene.curLv:setString(GetUtf8(curIntensify.name));
        scene.curAttr:setString(BuildAttr(curIntensify.attr))
                
        if curLv < nextLv then
            scene.gold:setString(nextIntensify.gold);
            scene.nextLv:setString(GetUtf8(nextIntensify.name));
            scene.nextAttr:setString(BuildAttr(nextIntensify.attr,curIntensify.attr))
        else
            scene.gold:setString(0);
            scene.nextLv:setString("Max");
            scene.nextAttr:setString("Max");
        end
        
        local g = g_Player.items[CONST.items.gold] >= nextIntensify.gold;
        local l = curLv < nextLv;
        
        SetNodeEnable(scene.intensify, l and g);
        scene.gold:setColor((l and not g) and cc.c3b(255,0,0) or oldColor);
        scene.nextLv:setColor((l and not g) and cc.c3b(255,0,0) or oldColor);
        scene.nextAttr:setColor((l and not g) and cc.c3b(255,0,0) or oldColor);
    end
    
    updateStatus();
    
    local function intensifyCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if curLv>=nextLv then
                return;
            end

            if ReduceItemData(CONST.items.gold, nextIntensify.gold) then
                FlushSitem();

                AddConfigData("equips", id, 1);

                updateStatus();
            end
        end
    end
    local button = scene.ui:getChildByName("intensify");
    button:addTouchEventListener(intensifyCallBack);
    SetButtonFunc(button, intensifyCallBack, "equip")
    
    local function equipCallBack(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            for k,v in pairs(EXCEL.equip) do
                local name = "equip"..k;                
                if name == sender:getName() then
                    id = k;
                    updateStatus();
                    break;
                end
           end
        end
    end

    for k,v in pairs(EXCEL.equip) do
        local name = "equip"..k;
        if scene[name] then
            scene[name]:addTouchEventListener(equipCallBack);
        end
    end
    
    return scene;
end

